﻿hpxAdminApp.controller('userWantBillsController', function ($scope, $rootScope, $stateParams, $state, API_URL, NgTableParams, constantsService, wantBillService, customerService, addressService) {
    $scope.filter = {
        showElectricBill: 1,
        showPaperBill: 0,
        acceptorTypeAll: true,
        biddingtypeAll: true,
        enterprise_name: $rootScope.identity.enterprise_name
    }
    $scope.model = {
        
    }
    $scope.model.bill_type_id = '101';
    $scope.model.acceptor_type_id = '';
    $scope.model.bidding_mode = '';
    $scope.model.contact_name = $rootScope.identity.customer_name;
    $scope.model.contact_phone = $rootScope.identity.phone_number;

    $scope.paper = {
        
    }
    $scope.paper.bill_type_id = '102';
    $scope.paper.acceptor_type_id = '';
    $scope.paper.bidding_mode = '';
    $scope.paper.contact_name = $rootScope.identity.customer_name;
    $scope.paper.contact_phone = $rootScope.identity.phone_number;

    $scope.electricBill = function () {
        $scope.filter.showElectricBill = 1;
        $scope.filter.showPaperBill = 0;
    }
    $scope.paperBill = function () {
        $scope.filter.showPaperBill = 1;
        $scope.filter.showElectricBill = 0;
    }
    //提取数字
    function extractNum(str) {
        str = str.replace(/[\u4E00-\u9FA5]/g, "")
        str = str.replace('(', ''),
        str = str.replace(')', '');
        var str_before = str.split("-")[0];
        var str_after = str.split("-")[1];
        return [str_before, str_after];
    }
    //选择票面金额
    $scope.checkBillPrice = function () {
        $scope.filter.bill_sum_price_min = extractNum($scope.model.bill_price)[0];
        $scope.filter.bill_sum_price_max = extractNum($scope.model.bill_price)[1];
    }

    //选择到期期限   
    $scope.checkBillDate = function () {
        $scope.filter.min_dead_date = extractNum($scope.model.bill_date)[0];
        $scope.filter.max_dead_date = extractNum($scope.model.bill_date)[1];
    }
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }
    //获取所有求购信息
    $scope.tableParams = new NgTableParams({ 'sorting': { 'create_time': 'desc' } }, {
        getData: function (params) {
            return wantBillService.getAllWantBill(params, $scope.filter).then(function (data) {
                $scope.first = $scope.getFirst(params);
                return data;
            });
        }
    });
    

    $scope.resd = function (item) {
        //弹出详情
        $scope.models = item;
        $('#modal-read').modal('show');
    }
    $scope.addWant = function () {
        //弹出发布窗口
        $('#modal-add').modal('show');
    }
    $scope.editWant = function (item) {
        // 修改求购
        console.log(item)
        if (item.bill_type_id == 101) {
            $scope.model = item;
        } else {
            $scope.paper = item;
        }
        $('#modal-edit').modal('show');
    }
    $scope.cityPublish = function () {
        //获取全部省级地址
        addressService.queryAll().then(function (data) {
            $scope.provinceData = data;
            $scope.provinceChange();
        });
        //获取各省市下面的市区
        $scope.provinceChange = function () {
            if (!$scope.paper.product_province_id) {
                $scope.cityData = [];
            } else if ($scope.paper.product_province_id == 1 || $scope.paper.product_province_id == 20 || $scope.paper.product_province_id == 860 || $scope.paper.product_province_id == 2462) {
                $scope.filter.tradeProvinceId = $scope.paper.product_province_id + 1;
                return addressService.queryCity($scope.filter.tradeProvinceId).then(function (data) {
                    $scope.cityData = data;
                });
            } else {
                return addressService.queryCity($scope.paper.product_province_id).then(function (data) {
                    $scope.cityData = data;
                });
            }
        }
    }
    $scope.cityPublish();
    //获取竞价方式信息-----
    //billService.getBiddingType().then(function (data) {
    //    $scope.biddingtypeData = data.slice(0, 2);
    //    $scope.biddingtypeAllData = [];
    //    for (var i = 0; i < $scope.biddingtypeData.length; i++) {
    //        $scope.biddingtypeAllData[i] = $scope.biddingtypeData[i];
    //    }
    //})
    //竞价方式全选
    //$scope.biddingTypeChangeAll = function () {
    //    for (var i = 0; i < $scope.biddingtypeData.length; i++) {
    //        if ($scope.filter.biddingtypeAll) {
    //            $scope.model.bidding_type_id = '';
    //            $scope.paper.bidding_type_id = '';
    //            $scope.biddingtypeData[i].checked = false;
    //        }
    //    }
    //}
    //选择某一竞价方式取消全选
    //$scope.biddingTypeChange = function () {
    //    var biddingtypeID = [];
    //    for (var i = 0; i < $scope.biddingtypeData.length;i++){
    //        if ($scope.biddingtypeData[i].checked) {
    //            biddingtypeID.push($scope.biddingtypeData[i].code)
    //            $scope.filter.biddingtypeAll = false;
    //        }
    //    }
    //    $scope.model.bidding_type_id = biddingtypeID.join(",");
    //    $scope.paper.bidding_type_id = biddingtypeID.join(",");
    //}
    //获取承兑机构类型
    constantsService.queryConstantsType(4).then(function (data) {
        $scope.acceptorTypeData = data;
        $scope.acceptorTypeData3 = [];
        $scope.acceptorTypeData4 = [];
        for (var i = 0; i < 3; i++) {
            $scope.acceptorTypeData3[i] = $scope.acceptorTypeData[i];
        };
        for (var j = 0; j < $scope.acceptorTypeData.length - 3; j++) {
            $scope.acceptorTypeData4[j] = $scope.acceptorTypeData[j + 3];
        }
    });
    //承兑机构全选
    $scope.acceptorTypeChangeAll = function () {
        for (var i = 0; i < $scope.acceptorTypeData.length; i++) {
            if ($scope.filter.acceptorTypeAll) {
                $scope.model.acceptor_type_id = '';
                $scope.paper.acceptor_type_id = '';
                $scope.acceptorTypeData[i].checked = false;
            }
        }
    };
    //选中某一机构取消全选
    $scope.acceptorTypeChange = function () {
        var acceptorTypeID = [];
        for (var i = 0; i < $scope.acceptorTypeData.length; i++) {
            if ($scope.acceptorTypeData[i].checked) {
                acceptorTypeID.push($scope.acceptorTypeData[i].code)
                $scope.filter.acceptorTypeAll = false;
            }
        }
        $scope.model.acceptor_type_id = acceptorTypeID.join(",");
        $scope.paper.acceptor_type_id = acceptorTypeID.join(",");
    };

    //获取电票瑕疵类型
    constantsService.queryConstantsType(19).then(function (data) {
        $scope.billFlawData = data;
        for (var i = 0; i < $scope.billFlawData.length; i++) {
            if ($scope.billFlawData[i].code == 1500) {
                $scope.billFlawData[i].checked = true;
                break;
            }
        }
    });
    //获取纸票瑕疵类型
    constantsService.queryConstantsType(15).then(function (data) {
        $scope.billFlawData2 = data;
        for (var i = 0; i < $scope.billFlawData2.length; i++) {
            if ($scope.billFlawData2[i].code == 1500) {
                $scope.billFlawData2[i].checked = true;
                break;
            }
        }
    });
    //电票，当选中无瑕疵时，其他选项均为false；反之，选中其他选项时，无瑕疵选项为false
    $scope.billFlawChange = function (item) {
        if (item.code == 1500) {
            item.checked = true;
            for (var i = 1; i < $scope.billFlawData.length; i++) {
                $scope.billFlawData[i].checked = false;
            }
        }
        else {
            for (var i = 0; i < $scope.billFlawData.length; i++) {
                if (i == 0) {
                    $scope.billFlawData[i].checked = true;
                }
                else {
                    if ($scope.billFlawData[i].checked) {
                        $scope.billFlawData[0].checked = false;
                    }
                }
            }
        }
    }
    //纸票，当选中无瑕疵时，其他选项均为false；反之，选中其他选项时，无瑕疵选项为false
    $scope.billFlawChange2 = function (item) {
        if (item.code == 1500) {
            item.checked = true;
            for (var i = 1; i < $scope.billFlawData2.length; i++) {
                $scope.billFlawData2[i].checked = false;
            }
        }
        else {
            for (var i = 0; i < $scope.billFlawData2.length; i++) {
                if (i == 0) {
                    $scope.billFlawData2[i].checked = true;
                }
                else {
                    if ($scope.billFlawData2[i].checked) {
                        $scope.billFlawData2[0].checked = false;
                    }
                }
            }
        }
    }
    $scope.changeMinPrice = function () {
        if ($scope.model.min_face_price > 100000 || $scope.model.min_face_price.length >= 12) {
            $scope.model.min_face_price = 100000;
        }
    }
    $scope.changeMaxPrice = function () {
        if ($scope.model.max_face_price > 100000 || $scope.model.max_face_price.length >= 12) {
            $scope.model.max_face_price = 100000;
        }
    }
    $scope.paperChangeMinPrice = function () {
        if ($scope.paper.min_face_price > 100000 || $scope.paper.min_face_price.length >= 12) {
            $scope.paper.min_face_price = 100000;
        }
    }
    $scope.paperChangeMaxPrice = function () {
        if ($scope.paper.max_face_price > 100000 || $scope.paper.max_face_price.length >= 12) {
            $scope.paper.max_face_price = 100000;
        }
    }
    //发布电票求购需求
    $scope.updataInfo = function () {
        var bill_flaw_ids = [];
        for (var i = 0; i < $scope.billFlawData.length; i++) {
            if ($scope.billFlawData[i].checked) {
                bill_flaw_ids.push($scope.billFlawData[i].code);
            }
        }
        $scope.model.falw_require = bill_flaw_ids.join(',');
        console.log($scope.model)
        wantBillService.addWantBill($scope.model).then(function () {
            alert("发布成功");
            $('#modal-add').modal('hide');
            $scope.tableParams.reload();
            $scope.model = {};
        })
    }
    //发布纸票求购需求
    $scope.updataPaperInfo = function () {
        var bill_flaw_ids = [];
        for (var i = 0; i < $scope.billFlawData2.length; i++) {
            if ($scope.billFlawData2[i].checked) {
                bill_flaw_ids.push($scope.billFlawData2[i].code);
            }
        }
        $scope.paper.falw_require = bill_flaw_ids.join(',');
        wantBillService.addWantBill($scope.paper).then(function () {
            alert("发布成功");
            $('#modal-add').modal('hide');
            $scope.tableParams.reload();
            $scope.model = {};
        })
    }

    // 修改求购
    $scope.editAdd = function (item) {
        //console.log(item)
        if ($scope.model.bill_type_id == 101) {
            var bill_flaw_ids = [];
            for (var i = 0; i < $scope.billFlawData.length; i++) {
                if ($scope.billFlawData[i].checked) {
                    bill_flaw_ids.push($scope.billFlawData[i].code);
                }
            }
            $scope.model.falw_require = bill_flaw_ids.join(',');
            console.log($scope.model)
            wantBillService.editWantBill($scope.model).then(function () {
                alert("修改成功");
                $('#modal-edit').modal('hide');
                $scope.tableParams.reload();
                $scope.model = {};
            })
        } else {
            var bill_flaw_ids = [];
            for (var i = 0; i < $scope.billFlawData2.length; i++) {
                if ($scope.billFlawData2[i].checked) {
                    bill_flaw_ids.push($scope.billFlawData2[i].code);
                }
            }
            $scope.paper.falw_require = bill_flaw_ids.join(',');
            wantBillService.editWantBill($scope.paper).then(function () {
                alert("修改成功");
                $('#modal-edit').modal('hide');
                $scope.tableParams.reload();
                $scope.paper = {};
            })
        }
    }

});
